import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

/// 首页overview 排序销售额
showSalesTrendBottomSheet(BuildContext context, String salesTrend) async {
  String _currentSelectCode = salesTrend;
  await showDefaultBottomSheet(
    context,
    child: SalesTrendBottomSheet(
      salesTrend: salesTrend,
      selectCallBack: (data) {
        _currentSelectCode = data;
      },
    ),
    title: S.current.sort_sales_trend,
    isScrollControlled: true,
  );
  return _currentSelectCode;
}

class SalesTrendBottomSheet extends StatefulWidget {
  final String salesTrend;
  final ValueChanged<String> selectCallBack;

  const SalesTrendBottomSheet({Key key, this.salesTrend, this.selectCallBack})
      : super(key: key);

  @override
  _SalesTrendBottomSheetState createState() => _SalesTrendBottomSheetState();
}

class _SalesTrendBottomSheetState extends State<SalesTrendBottomSheet> {
  List<String> salesTrendList = [
    S.current.daily,
    S.current.weekly,
    S.current.monthly,
  ];

  String currentSelectSalesTrend;

  @override
  void initState() {
    super.initState();
    currentSelectSalesTrend = widget.salesTrend;
  }

  void onChangeSalesTrend(String value) {
    if (currentSelectSalesTrend == value) return;
    setState(() {
      currentSelectSalesTrend = value;
    });
    widget.selectCallBack?.call(value);
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          height: getDp(17),
        ),
        _buildTop(S.current.sort_by_date_range),
        SizedBox(
          height: getDp(17),
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: getDp(35),
          ),
          child: Wrap(
            runSpacing: getDp(24),
            spacing: getDp(22),
            alignment: WrapAlignment.start,
            children: salesTrendList
                .map((e) => PressedOpacityButton(
                      onPressed: () => onChangeSalesTrend(e),
                      child: NormalRadio(
                        title: e,
                        isSelect: currentSelectSalesTrend == e,
                      ),
                    ))
                .toList(),
          ),
        ),
      ],
    );
  }

  Widget _buildTop(String title) => Container(
        height: getDp(32),
        color: aceWhiteColor,
        padding: EdgeInsets.symmetric(horizontal: getDp(24)),
        alignment: Alignment.centerLeft,
        child: Text(
          title,
          style: TextStyle(
            color: aceDarkGrayColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w400,
            height: 22 / 16,
          ),
        ),
      );
}
